//
//  LaunchView.m
//  crowd
//
//  Created by 陈磊 on 16/4/27.
//  Copyright © 2016年 caocaod.com. All rights reserved.
//

#import "LaunchView.h"
#import "Masonry.h"
#define BasePageTag 2000;

@interface LaunchView()<UIScrollViewDelegate>

@property (nonatomic, strong)UIScrollView *scrollView;

@end
@implementation LaunchView

- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {

            UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
            [self addSubview:backView];
            
            UIScrollView *scr = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
            self.scrollView = scr;
            [backView addSubview:scr];
            
            scr.delegate = self;
            
            NSArray *nameArr;
            
            if(VIEW_HEIGHT == 480.0 ) {
                nameArr = @[@"new01",@"new02",@"new03"];;
            } else {
                nameArr = @[@"1",@"2",@"3"];
            }
            
            [scr setContentOffset:CGPointZero];
            [scr setContentSize:CGSizeMake(frame.size.width * nameArr.count, 0)];
            [scr setBounces:NO];
            [scr setShowsVerticalScrollIndicator:NO];
            [scr setShowsHorizontalScrollIndicator:NO];
            [scr setPagingEnabled:YES];
            
            
            for(NSInteger i = 0 ; i < nameArr.count ; i ++) {
                if (i == 2) {
                    UIButton *button  = [UIButton buttonWithType:UIButtonTypeCustom];
                    [button setBackgroundImage:[UIImage imageNamed:nameArr[i]]forState:UIControlStateNormal];
                    button.frame = CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height);
                    [scr addSubview:button];
                    [button addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
                    
                } else {
                    
                    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height)];
                    [imageView setImage:[UIImage imageNamed:nameArr[i]]];
                    imageView.userInteractionEnabled = YES;
                    [scr addSubview:imageView];
                    
                }
                
            }
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            WS(ws);
            btn.titleLabel.font = [UIFont systemFontOfSize:15];
            [btn setFrame:CGRectMake(ws.frame.size.width - 60, 20, 40, 20)];
            
            
            btn.titleLabel.textAlignment = NSTextAlignmentRight;
            [btn setTitleColor:RGB(205, 205, 205) forState:UIControlStateNormal];
            [btn setTitle:@"跳过>" forState:UIControlStateNormal];
            [self addSubview:btn];
            
            [btn addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
            
            UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(0,frame.size.height - 60, frame.size.width,60)];
            page.currentPage = 0;
            page.numberOfPages = nameArr.count;
            page.tag = BasePageTag;
            page.currentPageIndicatorTintColor = RGB(153, 153, 153);
            page.pageIndicatorTintColor = RGB(200, 200, 200);
            
            [backView addSubview:page];
    }
    return self;
}

- (void)pressAction:(UIButton *)sender {
    [UIView animateWithDuration:1 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark- UIScrollViewDeleGate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGPoint offSize = scrollView.contentOffset;
    UIPageControl *page = (UIPageControl *)[scrollView.superview viewWithTag:2000];
    
    page.currentPage = offSize.x / [UIScreen mainScreen].bounds.size.width;
}
@end
